
#include "viewing.h"

static char PosStr[15];
static HWND xpos, xposb, ypos, yposb, zpos, zposb;
static HWND xrot, xrotb, yrot, yrotb, zrot, zrotb;

// If the value is greater than the allowed maximum view position
// then reduce it down.
void AdjustViewPos (float *value, float MAX)
{
	if (*value > MAX)
		*value = MAX;
}

// Set the buddies for the respective edit and up down controls
// and set the range for the controls
void InitBuddies (HWND *edit, HWND *updown, UINT eid, UINT udid,
		HWND hWnd, int MAX)
{
	*edit = GetDlgItem (hWnd, eid);
	*updown = GetDlgItem (hWnd, udid);
	SendMessage (*updown, UDM_SETBUDDY, (LONG)(*edit), 0L);
	SendMessage (*updown, UDM_SETRANGE, 0L,
		MAKELONG (0, (int)(MAX*10)));
}

// Get the values out of the floats and put them in their respective
// edit controls and adjust the up down control
void SetBuddyTexts (float value, UINT edit, HWND editb, HWND hWnd)
{
	sprintf_s (PosStr, "%.1f", value);
	SetDlgItemText (hWnd, edit, PosStr);
	SendMessage (editb, UDM_SETPOS, 0, 
		(LPARAM) MAKELONG ( (int)(value*10), 0));
}

// Save the values into the respective floats and then adjust the 
// up down current position
void SavePosValues (float *value, UINT edit, HWND editb, HWND hWnd, 
					float MAX)
{
	GetDlgItemText (hWnd, edit, PosStr, 15);
	sscanf_s (PosStr, "%f", value);
	AdjustViewPos (value, MAX);
	SendMessage (editb, UDM_SETPOS, 0, 
		(LPARAM) MAKELONG ( (int)(*value*10), 0));
}

// When the scroll button is hit, the edit boxes and the float
// values have to be updated
void AdjustFromScrolling (float *value, WPARAM wParam, HWND hWnd,
						  UINT edit)
{
	*value = (float)((float)HIWORD(wParam)/10.0);
	sprintf_s (PosStr, "%.1f", *value);
	SetDlgItemText (hWnd, edit, PosStr);
}

BOOL APIENTRY ViewingConfigureDialog (HWND hDlg, UINT message,
		UINT wParam, LONG lParam)
{
	static PROPSHEETPAGE *ps;

	switch (message)
	{
	case WM_INITDIALOG:
		ps = (PROPSHEETPAGE*) lParam;

		// Set the buddies for the viewing position
		InitBuddies (&xpos, &xposb, IDC_POSX, IDC_XPOSB, hDlg, MAXVIEWPOS);
		InitBuddies (&ypos, &yposb, IDC_POSY, IDC_YPOSB, hDlg, MAXVIEWPOS);
		InitBuddies (&zpos, &zposb, IDC_POSZ, IDC_ZPOSB, hDlg, MAXVIEWPOS);

		// Set the buddies for the rotation position
		InitBuddies (&xrot, &xrotb, IDC_ROTX, IDC_ROTXB, hDlg, MAXROT);
		InitBuddies (&yrot, &yrotb, IDC_ROTY, IDC_ROTYB, hDlg, MAXROT);
		InitBuddies (&zrot, &zrotb, IDC_ROTZ, IDC_ROTZB, hDlg, MAXROT);
		return TRUE;
		break;

	case WM_NOTIFY:
		switch ( ( (NMHDR FAR *)lParam) -> code)
		{
		case PSN_SETACTIVE:
			// Set the values of the x, y, and z viewing positions
			// from the value loaded
			SetBuddyTexts (x_viewpos, IDC_POSX, xposb, hDlg);
			SetBuddyTexts (y_viewpos, IDC_POSY, yposb, hDlg);
			SetBuddyTexts (z_viewpos, IDC_POSZ, zposb, hDlg);

			// Set the rotation direction in the x, y and z dirction
			SetBuddyTexts (x_rotdir, IDC_ROTX, xrotb, hDlg);
			SetBuddyTexts (y_rotdir, IDC_ROTY, yrotb, hDlg);
			SetBuddyTexts (z_rotdir, IDC_ROTZ, zrotb, hDlg);
			break;

		case PSN_APPLY:
			// Save the values into the INI file
			SaveViewingSettings();
			break;

		case PSN_KILLACTIVE:
			// Get the values out of the text box and save the value
			// as it is not active any more

			// for the viewing position
			SavePosValues (&x_viewpos, IDC_POSX, xposb, hDlg, 
				(float)MAXVIEWPOS);
			SavePosValues (&y_viewpos, IDC_POSY, yposb, hDlg, 
				(float)MAXVIEWPOS);
			SavePosValues (&z_viewpos, IDC_POSZ, zposb, hDlg, 
				(float)MAXVIEWPOS);

			// for the rotation direction
			SavePosValues (&x_rotdir, IDC_ROTX, xrotb, hDlg, 
				(float)MAXROT);
			SavePosValues (&y_rotdir, IDC_ROTY, yrotb, hDlg,
				(float)MAXROT);
			SavePosValues (&z_rotdir, IDC_ROTZ, zrotb, hDlg,
				(float)MAXROT);
			break;
		}
		return TRUE;
		break;

	case WM_VSCROLL:
		// Get the scroll messages and update the buddy text boxes
		// by ourselves.
		if ((HWND)lParam == xposb)
		{
			AdjustFromScrolling (&x_viewpos, wParam, hDlg, IDC_POSX);	
		}
		else if ((HWND)lParam == yposb)
		{
			AdjustFromScrolling (&y_viewpos, wParam, hDlg, IDC_POSY);
		}
		else if ((HWND)lParam == zposb)
		{
			AdjustFromScrolling (&z_viewpos, wParam, hDlg, IDC_POSZ);
		}
		else if ((HWND)lParam == xrotb)
		{
			AdjustFromScrolling (&x_rotdir, wParam, hDlg, IDC_ROTX);
		}
		else if ((HWND)lParam == yrotb)
		{
			AdjustFromScrolling (&y_rotdir, wParam, hDlg, IDC_ROTY);
		}
		else if ((HWND)lParam == zrotb)
		{
			AdjustFromScrolling (&z_rotdir, wParam, hDlg, IDC_ROTZ);
		}
		return TRUE;
		break;
	}
	return FALSE;
}